var form;
var layer;
//页面初始化时加载Layui框架
layui.use(['form', 'layer', 'laydate'], function () {
    form = layui.form();
    layer = layui.layer;

    //加载产品类型
    initDict("product_type", "list");

    $.removeTableCookie('SpryMedia_DataTables_productionTable_productionList.jsp');
    getDataTable();

    //新增
    $("#addProduction").click(function () {
        saveOrUpdateProduction(null);
    });
    // 查询
    $("#search").click(function () {
        $.removeTableCookie('SpryMedia_DataTables_productionTable_productionList.jsp');
        getDataTable(); //要触发的方法
        return false;
    });

    //回车事件
    $(document).keydown(function (event) {
        var code = event.which;
        if (code == 13) {
            $.removeTableCookie('SpryMedia_DataTables_productionTable_productionList.jsp');
            getDataTable(); //要触发的方法
            return false;
        }
    });
});
/**
 * 获取列表
 */
function getDataTable() {
    $('#productionTable').dataTable({
        'bProcessing': true,
        'bServerSide': true,
        'fnServerParams': function (aoData) {
            var batch = $.trim($("#batch").val());
            var productType = $("#productType").val();
            var state = $("#state").val();
            aoData.push({
                "name": "production.batch",
                "value": batch
            }, {
                "name": "production.productType",
                "value": productType
            }, {
                "name": "production.state",
                "value": state
            });
        },
        "sAjaxSource": basePath + "production/getProductionList.action", // 获取列表
        "sServerMethod": "POST",
        "sPaginationType": "full_numbers",
        "bPaginate": true, // 翻页功能
        "bStateSave": true, // 状态保存
        "bLengthChange": false, // 改变每页显示数据数量
        "bFilter": false, // 过滤功能
        "bSort": false, // 排序功能
        "bInfo": true, // 页脚信息
        "bAutoWidth": false, // 自动宽度
        "bDestroy": true,
        "iDisplayLength": 15, // 每页显示多少行
        "aoColumns": [{
            "sTitle": '排序',
            "mDataProp": "no"
        }, {
            "sTitle": '生产批次',
            "mDataProp": "batch"
        }, {
            "sTitle": '产品类型',
            "mDataProp": "productTypeStr"
        }, {
            "sTitle": '开始时间',
            "mDataProp": "startTime"
        }, {
            "sTitle": '结束时间',
            "mDataProp": "entTime"
        }, {
            "sTitle": '生产数量',
            "mDataProp": "number"
        }, {
            "sTitle": '合格数量',
            "mDataProp": "qualified"
        }, {
            "sTitle": '备注',
            "mDataProp": "remark",
            "sClass": "conceal"
        }, {
            "sTitle": '状态',
            "mDataProp": "stateStr"
        }, {
            "sTitle": '负责人',
            "mDataProp": "chargeName"
        }, {
            "sTitle": '操作',
            "mDataProp": null
        }],
        "oLanguage": {
            "sUrl": basePath + "plugins/datatable/cn.txt" // 中文包
        },
        "fnDrawCallback": function (oSettings) {
            $('#productionTable tbody  tr td').each(function () {
                this.setAttribute('title', $(this).text());
            });
        },
        "fnInitComplete": function () {
        },
        "aoColumnDefs": [{
            "aTargets": [1],
            "fnRender": function (oObj) {
                var batch = oObj.aData.batch;
                var overdue = oObj.aData.overdue;
                var state = oObj.aData.state;
                if (overdue == 1 && state == 0) {
                    return '<span style="color: #990000">[逾期]</span>' + batch;
                } else {
                    return batch;
                }
            }
        }, {
            "aTargets": [10],
            "fnRender": function (oObj) {
                var pId = oObj.aData.id;
                var state = oObj.aData.state;//0：未开始 1：进行中 2：已完成 3：已质检 4：已入库
                var html = '<a data-method="authorize" class="layui-btn layui-btn-small layui-btn-problem layui-btn-radius" onclick=productDetails(\'' + pId + '\')>生产产品</a>';
                if (state == 0) {
                    html += '<a data-method="repair" class="layui-btn layui-btn-small layui-btn-normal layui-btn-radius" onclick=saveOrUpdateProduction(\'' + pId + '\')>修改</a>'
                        + '<a data-method="del" class="layui-btn layui-btn-small layui-btn-danger layui-btn-radius" onclick=deleteProduction(\'' + pId + '\')>删除</a>';
                } else if (state == 1) {
                    html += '<a data-method="complete" class="layui-btn layui-btn-small layui-btn-normal layui-btn-radius" onclick=productionFinish(\'' + pId + '\')>完成</a>'
                        + '<a data-method="del" class="layui-btn layui-btn-small layui-btn-radius layui-btn-disabled" >删除</a>';
                } else if (state == 2) {
                    html += '<a data-method="check" class="layui-btn layui-btn-small layui-btn-normal layui-btn-radius" onclick=applyCheck(\'' + pId + '\')>质检</a>'
                        + '<a data-method="del" class="layui-btn layui-btn-small layui-btn-radius layui-btn-disabled" >删除</a>';
                } else if (state == 3) {
                    html += '<a data-method="complete" class="layui-btn layui-btn-small layui-btn-normal layui-btn-radius" onclick=applyInStock(\'' + pId + '\')>入库</a>'
                        + '<a data-method="del" class="layui-btn layui-btn-small layui-btn-radius layui-btn-disabled" >删除</a>';
                }else{
                    html += '<a data-method="repair" class="layui-btn layui-btn-small layui-btn-radius layui-btn-disabled" >修改</a>'
                        + '<a data-method="del" class="layui-btn layui-btn-small layui-btn-radius layui-btn-disabled" >删除</a>';
                }
                return html;
            }
        }]
    });
}
/**
 * 跳转到产品页面
 */
function productDetails(pId) {
    var url = "jsp/product/productList.jsp?pId=" + pId;
    var titleVal = "生产产品";
    layer.open({
        type: 2,
        title: titleVal,
        area: ["100%", "100%"],
        shade: 0.3,
        content: basePath + url,
        zIndex: layer.zIndex,
        success: function (layero, index) {//弹出后回调
            layer.setTop(layero); //弹窗置于最上层
            layer.full(index);
        }
    });
}
/**
 * 新增/修改
 */
function saveOrUpdateProduction(pId) {
    var url;
    var titleVal;
    if (null == pId) {
        url = "jsp/production/addOrUpdateProduction.jsp";
        titleVal = "新增生产信息";
    } else {
        url = "production/toUpdateOrDetail.action?type=update&id=" + pId;
        titleVal = "修改生产信息";
    }
    layer.open({
        type: 2,
        title: titleVal,
        area: ["50%", "55%"],
        shade: 0.3,
        maxmin: true,
        content: basePath + url,
        zIndex: layer.zIndex,
        success: function (layero) {//弹出后回调
            layer.setTop(layero); //弹窗置于最上层
        }
    });
}

/**
 * 删除
 */
function deleteProduction(pId) {
    layer.confirm('删除生产信息会同时删除对应的所有产品信息,确定删除此条数据?', {
        title: " 确认",
        icon: 3,
        skin: 'layui-layer-molv',
        btn: ["确定", "取消"],
        yes: function (index) {
            $.ajax({
                url: basePath + "production/deleteProduction.action?id=" + pId,
                type: "post",
                dataType: 'text',
                success: function (data) {
                    if (data != null && data != "" && data == 0) {
                        getDataTable();
                    } else {
                        layer.open({
                            title: '错误',
                            icon: 2,
                            content: '删除失败！'
                        });
                        getDataTable();
                    }
                }
            });
            layer.close(index);
        }
    });
}

/**
 * 生产完成
 * @param pId
 */
function productionFinish(pId){

}

/**
 * 申请质检
 * @param pId
 */
function applyCheck(pId){

}
/**
 * 申请入库
 * @param pId
 */
function applyInStock(pId){

}